/*
 * CollectGoodsListItem.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.event.CollectGoodsEvent;
import com.yidiandao.business.data.model.GoodsModel;
import com.yidiandao.business.data.model.IGoodsModel;
import com.yidiandao.business.ui.collect.CollectActivity;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.main.shoppingcart.confirmorder.ConfirmOrderActivity;
import com.yidiandao.business.ui.widget.dialog.BusinessDialog;
import com.yidiandao.business.ui.widget.dialog.LoadingDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.CollectGoodsHolder;
import com.yidiandao.business.ui.widget.view.GoodsCountMenu;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * 收藏 - 商品 - ListItem
 */
public class CollectGoodsListItem extends DisplayListItem<CollectGoodsHolder, Goods> {

    private final GoodsModel mGoodsModel;

    private Activity mActivity;
    private GoodsCountMenu mGoodsCountMenu;

    public CollectGoodsListItem(Goods s) {
        super(s);
        mGoodsModel = new GoodsModel();
    }

    @Override
    public void onShow(final Context context, final CollectGoodsHolder holder) {
        mActivity = (Activity) context;

        //logo
        ImageLoader.loadImage(context, holder.mIvGoodsImage, data.getGoodsThumb());
        //品名
        holder.mTvTitle.setText(data.getGoodsName());
        //价格
        holder.mTvPrice.setText(data.getShopPrice());

        //取消收藏
        holder.mTvCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UmengUtils.onEvent(context, R.string.umeng_collect_cancle);
                CollectActivity activity = (CollectActivity) context;

                new BusinessDialog.Builder(activity)
                        .setTitle(R.string.dialog_collectTip)
                        .setPositiveButton(R.string.dialog_sure, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                EventBus.getDefault().post(new CollectGoodsEvent(data.getGoodsId(), CollectGoodsListItem.this));
                            }
                        })
                        .setNegativeButton(R.string.dialog_cancel, null)
                        .create()
                        .show();
            }
        });
        if (data.getCanBuy() == 0
                || data.getStockNumber() == 0
                || data.getLimitNumber() == 0
                || data.getMinNumber() > data.getStockNumber()) {
            if (data.getCanBuy() == 0) {
                holder.mTvStatus.setVisibility(View.VISIBLE);
                holder.mLlGoodsInfo.setOnClickListener(null);
            } else {
                holder.mTvStatus.setVisibility(View.INVISIBLE);
            }
            holder.mTvBuy.setTextColor(UIUtils.getColor(R.color.text_hint_color));
            holder.mTvBuy.setBackgroundResource(R.drawable.bg_btn_stroke_gray_normal);
            holder.mTvBuy.setOnClickListener(null);
        } else {
            holder.mTvStatus.setVisibility(View.INVISIBLE);
            holder.mTvBuy.setTextColor(UIUtils.getColor(R.color.sl_btn_red_text));
            holder.mTvBuy.setBackgroundResource(R.drawable.sl_btn_stroke_red);

            //购买
            holder.mTvBuy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showChooseGoodsCountPopWindow(holder.getParent());
                }
            });
            //查看信息
            holder.mLlGoodsInfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UmengUtils.onEvent(context, R.string.umeng_collect_detail);
                    GoodsDetailActivity.actionStart((Activity) context, data.getGoodsId());
                }
            });
        }
    }

    private void showChooseGoodsCountPopWindow(View parent) {

        final PopupWindow popupWindow = new PopupWindow(mActivity);
        View view = View.inflate(mActivity, R.layout.dialog_collect_goods, null);
        popupWindow.setWidth(-1);
        popupWindow.setHeight(-1);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setContentView(view);

        View viewPopBg = view.findViewById(R.id.view_pop_bg);
        TextView tvPopNumber = (TextView) view.findViewById(R.id.tv_number);
        ImageView ivLogo = (ImageView) view.findViewById(R.id.iv_logo);
        TextView tvName = (TextView) view.findViewById(R.id.tv_name);
        TextView tvPrice = (TextView) view.findViewById(R.id.tv_price);
        mGoodsCountMenu = (GoodsCountMenu) view.findViewById(R.id.goods_count_menu);
        TextView tvBuyNow = (TextView) view.findViewById(R.id.tv_buy_now);
        ImageView ivFinish = (ImageView) view.findViewById(R.id.iv_finish);

        tvName.setText(data.getGoodsName());
        tvPopNumber.setText(UIUtils.getString(R.string.goodsdetail_number, data.getStockNumber()));
        tvPrice.setText(data.getShopPrice());
        if (data.getLimitNumber() > 0) {
            mGoodsCountMenu.setMaxGoodsCount(data.getLimitNumber());
        } else if (data.getLimitNumber() < 0) {
            mGoodsCountMenu.setMaxGoodsCount(data.getStockNumber());
        }
        mGoodsCountMenu.setMinGoodsCount(data.getMinNumber());
        mGoodsCountMenu.setGoodsCount(data.getMinNumber());

        ImageLoader.loadImage(mActivity, ivLogo, data.getGoodsThumb());

        // 华为手机适配
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);

        viewPopBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        ivFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        tvBuyNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 修改为最小起订量
                if (mGoodsCountMenu.getGoodsCount() < mGoodsCountMenu.getMinGoodsCount()) {
                    UIUtils.showToast(UIUtils.getString(R.string.min_number, mGoodsCountMenu.getMinGoodsCount()));
                    mGoodsCountMenu.setGoodsCount(mGoodsCountMenu.getMinGoodsCount());
                    return;
                }
                popupWindow.dismiss();

                //                if (Integer.parseInt(data.getLimitNumber()) == -1) {

                final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();

                mGoodsModel.buyNow(getDisplayItemTag(), data.getGoodsId(), mGoodsCountMenu.getGoodsCount(), new IGoodsModel.onBuyNowListener() {

                    @Override
                    public void onSuccess(String json, String recId, int goodsNumber, float goodsPrice) {
                        //                        // 添加商品到本地购物车
                        //                        addToMemoryShoppingCart
                        // (recId, goodsNumber, goodsPrice);

                        // 收藏的购买
                        // 商品信息在 信息在 ListItem 绑定的Data里面
                        ConfirmOrderActivity.actionStart(mActivity, false);
                    }

                    @Override
                    public void onFailed() {
                    }

                    @Override
                    public void onFinal() {
                        loadingDialog.dismiss();
                        popupWindow.dismiss();
                    }
                });
                //                } else if (Integer.parseInt(data.getLimitNumber()) > 0) {
                //                    ConfirmOrderActivity.actionStartForActiveGoods(mActivity, data.getGoodsId(), goodsCountMenu.getGoodsCount(), data.getGoodsType());
                //                }
            }
        });
    }

    //    public void addToMemoryShoppingCart(String recId, int goodsNumber, float goodsPrice) {
    //        Shop shop = new Shop();
    //        shop.setSupplierId(data.getSupplierId());
    //        shop.setSupplierName(data.getSupplierName());
    //        shop.setShopLogo(data.getShopLogo());
    //
    //        data.setRecId(recId);
    //        data.setGoodsNumber(goodsNumber);
    //        data.setGoodsTotalPrice(goodsPrice);
    //
    //        // 添加商品到购物车
    //        ShoppingCartManager.getInstance().addGoodsToShoppingCart(shop, data);
    //    }

    @Override
    public String getDisplayItemTag() {
        return CollectGoodsListItem.class.getSimpleName();
    }

    @Override
    public Class<CollectGoodsHolder> getViewHolderClazz() {
        return CollectGoodsHolder.class;
    }
}
